package fr.lgm.server;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import fr.lgm.client.ScheduleRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import fr.lgm.shared.Members;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ScheduleRequestImpl extends RemoteServiceServlet implements
		ScheduleRequest {

	
	
	
public String greetServer(String input) throws IllegalArgumentException {
		
		
		Connection conn = null;

		// parameters
		String url      = "jdbc:mysql://localhost/";
		String db       = "grbl";
		String driver   = "com.mysql.jdbc.Driver";
		String user     = "root";
		String pass     = "xxx";    
		url = url + db;		 
    
		try {

            	Class.forName(driver).newInstance();
            	conn = DriverManager.getConnection(url, user, pass);

		} catch (Exception e) {
            
            	System.err.println("Mysql Connection Error: ");
            	// for debugging error
            	e.printStackTrace();
		}

		if (conn == null)  {
            	System.out.println("~~~~~~~~~~ can't get a Mysql connection");
		}		
		
		/*if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}*/

		
		Statement select = null;
		
		try {
			select = conn.createStatement();
		
			ResultSet result=null;
			result = select.executeQuery("select * from user");				
			
			while (result.next()) {
			    input = input+"<tag>"+ result.getString(1)+"</tag>";     
			}
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}       
        
        return "" + input;
	}

	

	
public Members[] getUser() throws IllegalArgumentException {
		
		
		Connection conn = null;

		// parameters
		String url      = "jdbc:mysql://localhost/";
		String db       = "grbl";
		String driver   = "com.mysql.jdbc.Driver";
		String user     = "root";
		String pass     = "jeromenoob7";   
		Members[] res   = new Members[20];
		int i           = 0;
		//int size      = 0; //size of Users db table
		url = url + db;		 
    
		
		try {

            	Class.forName(driver).newInstance();
            	conn = DriverManager.getConnection(url, user, pass);

		} catch (Exception e) {
            
            	System.err.println("Mysql Connection Error: ");
            	// for debugging error
            	e.printStackTrace();
		}

		if (conn == null)  {
            	System.out.println("~~~~~~~~~~ can't get a Mysql connection");
		}		
		
		

		
		Statement select = null;
		
		try {
			select = conn.createStatement();
			ResultSet result=null;
			i = 0;
			
			result = select.executeQuery("select * from user");				
			
			while (result.next())
			{
				res[i] = new Members();
				res[i].newMembers(result.getString(1),result.getString(2),result.getString(5));
				i++;
			}
			return res;
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}       
        
        return res;
	}



public String[] Lire3() throws IllegalArgumentException {
	FileReader monFichier = null;
	BufferedReader tampon = null;
	
	try {
		monFichier = new FileReader("C:/Users/Jérôme/Desktop/test.csv");
		tampon = new BufferedReader(monFichier);

		while (true) {
		// Lit une ligne de test.csv
		String ligne = tampon.readLine();
		// Vérifie la fin de fichier
		if (ligne == null)
		break;
		//System.out.println(ligne);
		} // Fin du while
		} catch (IOException exception) {
		exception.printStackTrace();
		} finally {
		try {
		tampon.close();
		monFichier.close();
		} catch(IOException exception1) {
		exception1.printStackTrace();
				}
			}
	return null;
}


public String[] getCurrentMonth() throws IllegalArgumentException {
	
	String[]res = new String[15];
	Calendar c = new GregorianCalendar();
	c.setTime(new Date());
	int i=0;
	
	while (c.getActualMinimum(Calendar.DAY_OF_MONTH) < c.getActualMinimum(GregorianCalendar.DAY_OF_MONTH)){
		
		res[i] = new String();	
		res[i] = Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+i);
		i++;
	
	}
	return res;
	
	/*res[3]=Integer.toString(c.get(Calendar.DAY_OF_MONTH)+1);
	res[2]=Integer.toString(c.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
	res[0]=Integer.toString(c.getActualMinimum(GregorianCalendar.DAY_OF_MONTH));
	res[1]=Integer.toString(c.get(Calendar.DAY_OF_MONTH));
	res[1]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH));
	res[2]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+1);
	res[3]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+2);
	res[4]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+3);
	res[5]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+4);
	res[6]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+5);
	res[7]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+6);
	res[8]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+7);
	res[9]=Integer.toString(c.getActualMinimum(Calendar.DAY_OF_MONTH)+8);*/
	
}

	/*
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}*/
}